package com.atguigu.spzx.model.vo.common;

import lombok.Data;

/**
 * @author LT
 * @create 2024-03-06-21:13
 */
@Data
public class Result<T> {
    //状态码 200代表成功
    private Integer code;
    //状态码的描述
    private String message;
    //成功的响应数据
    private T data;

    private Result() {
    }

    public static <T> Result<T> ok(T data) {
        Result<T> result = Result.setResult(ResultCodeEnum.SUCCESS);
        result.data = data;
        return result;
    }
    public static <T> Result<T> ok() {
        Result<T> result = Result.setResult(ResultCodeEnum.SUCCESS);
        result.data = null;
        return result;
    }

    public static <T> Result<T> fail() {
        return Result.setResult(ResultCodeEnum.ERROR);
    }

    public static <T> Result<T> setResult(Integer code, String message) {
        Result<T> result = new Result();
        result.setMessage(message);
        result.setCode(code);
        return result;
    }

    public static <T> Result<T> setResult(ResultCodeEnum codeEnum) {
        Result<T> result = new Result();
        result.setMessage(codeEnum.getMessage());
        result.setCode(codeEnum.getCode());
        return result;
    }

    public Result<T> code(Integer code) {
        this.setCode(code);
        return this;
    }

    public Result<T> message(String message) {
        this.setMessage(message);
        return this;
    }

    public Result<T> data(T data) {
        this.setData(data);
        return this;
    }
}
